package com.iflysse.bbs.dao.impl;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.iflysse.bbs.dao.ReplyDao;
import com.iflysse.bbs.model.ReplyInfo;
import com.iflysse.bbs.util.JdbcUtil;

public class ReplyDaoImpl implements ReplyDao {
	private QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
	
	@Override
	public boolean add(ReplyInfo reply) {
		boolean flag = false;
		String sql = "insert into reply(content,date,userid,newsid) values(?,?,?,?)";
		Object[] params = {reply.getContent(),reply.getDate(),reply.getUserId(),reply.getNewsId()};
		try {
			int row = runner.update(sql, params);
			flag = row > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public List<ReplyInfo> getReplyListByNewsId(int newsId) {
		List<ReplyInfo> list = null;
		String sql = "select reply.*,user.name as userName from reply left join user on reply.userid=user.id where reply.newsid=?";
		try {
			list = runner.query(sql, new BeanListHandler<ReplyInfo>(ReplyInfo.class),newsId);
		} catch (Exception e) {
			e.printStackTrace();			
		}
		return list;
	}

	@Override
	public boolean deleteByNewsId(int newsId) {
		boolean flag = false;
		String sql = "delete from reply where newsid=?";
		try {
			int row = runner.update(sql, newsId);
			flag = row >= 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

}
